package com.masterrabbit.gyf.Conf;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitMqProducerConf {

    //先声明队列
    @Bean("q1")
    public Queue getQueue1() {
    return QueueBuilder.durable("myq1").ttl(10000).deadLetterExchange("ex1").build();
    }

    //声明一个死信队列
    @Bean("DeadQueue1")
    public Queue getQueue2() {
        return QueueBuilder.durable("myq2").build();
    }

    //创建一个死信队列交换机
    @Bean("DeadEx1")
    public Exchange getExchange1() {
        return ExchangeBuilder.fanoutExchange("ex1").durable(true).build();
    }

    //开始绑定死信队列和交换机
    @Bean
    public Binding getBinding1() {
        return BindingBuilder.bind(getQueue2()).to(getExchange1()).with("").noargs();
    }


    //优惠券抢成功后开始往mq中发送消息，用于插入mysql
    @Bean("SuccessMq")
    public Queue getSuccessQueue1() {
        return QueueBuilder.durable("successmq1").build();
    }
    @Bean("SuccessEx1")
    public Exchange getSuccessExchange1() {
        return ExchangeBuilder.fanoutExchange("successex1").durable(true).build();
    }
    @Bean
    public Binding getBinding2() {
        return BindingBuilder.bind(getSuccessQueue1()).to(getSuccessExchange1()).with("").noargs();
    }


    // 声明rabbitAdmin 的bean
    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }
}
